<?php

namespace App\Services\SwitchPay;

use App\Models\Orders;
use App\Services\GameChannelService;
use Illuminate\Http\Request;

class SwitchPayBase
{
    protected $payPlatform; // 支付渠道
    protected $gameId;
    protected $channel;
    protected $gameChannel;
    protected $payNotify;

    function __construct($pay_platform, $game_id, $game_channel_id)
    {
        $this->payPlatform = $pay_platform;
        $this->gameId      = $game_id;
        $this->gameChannel = GameChannelService::getInstance()->get($game_channel_id);
        $this->channel     = $this->gameChannel['channel'];
        $this->payNotify   = new PayNotify($game_id, $this->channel);
    }

    // 支付成功后端异步回调
    public function callbackUrl()
    {
        return config('my.switch_pay_url') . "/api/switch_pay/callback/" . $this->payPlatform . '/' . $this->gameChannel['id'];
    }

    // 支付成功前端回调
    public function callbackCpUrl()
    {
        return config('my.switch_pay_url') . "/switch_pay/callback/" . $this->payPlatform . '/' . $this->gameChannel['id'];
    }

    // 支付页面url
    public function payIndexUrl(Orders $order)
    {
        $pay_index_url = config('my.switch_pay_url') . "/switch_pay/index/" . $order->trade_no;

        return $pay_index_url;
    }

    // 支付完成返回显示页面url
    public function payCompleteUrl(Orders $order)
    {
        $pay_index_url = config('my.switch_pay_url') . "/switch_pay/complete/" . $order->trade_no;

        return $pay_index_url;
    }

    // 创建订单-渠道信息整理返回
    public function payInit(Orders $order, Request $request)
    {
        //
    }

    // 处理充值回调
    public function payCb(Request $request, $cb_type)
    {
        //
    }

    // 失败返回
    public function payErr()
    {
        //
    }

    // 成功返回
    public function payOk()
    {
        //
    }

    // 支付成功补齐订单其他信息
    public function supplementOrderInfo()
    {
        return true;
    }

    // 根据渠道获取处理对象
    public static function loadPay($pay_platform, $game_id, $game_channel_id)
    {
        $sdk_class = Orders::getSdkClassName($pay_platform);
        if (empty($sdk_class)) {
            return null;
        }
        $sdk_class_path = app_path("Services/SwitchPay/{$sdk_class}.php");
        if (!file_exists($sdk_class_path)) {
            return null;
        }

        $class = "App\\Services\\SwitchPay\\{$sdk_class}";
        return new $class($pay_platform, $game_id, $game_channel_id);
    }
}
